﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;

namespace Common {
    public class MeshLoader {
        public static ModelMesh LoadObjIndexed(string path) {
            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<Vector3> normalList = new List<Vector3>();
            List<Vector3> indexedVertexList = new List<Vector3>();
            List<Vector2> indexedUvList = new List<Vector2>();
            List<Vector3> indexedNormalList = new List<Vector3>();
            List<ushort> indexList = new List<ushort>();

            ObjLoader.LoadObj(path, vertexList, uvList, normalList);
            VboIndexHelper.IndexVbo(vertexList, uvList, normalList, indexList, indexedVertexList, indexedUvList, indexedNormalList);
            ModelMesh mesh = new ModelMesh();
            mesh.Indices = indexList.ToArray();
            mesh.Vertices = indexedVertexList.ToArray();
            mesh.Uvs = indexedUvList.ToArray();
            mesh.Normals = indexedNormalList.ToArray();
            return mesh;
        }
        public static ModelMesh LoadObjSimple(string path) {
            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<Vector3> normalList = new List<Vector3>();
            ObjLoader.LoadObj(path, vertexList, uvList, normalList);
            ModelMesh mesh = new ModelMesh();
            mesh.Vertices = vertexList.ToArray();
            mesh.Uvs = uvList.ToArray();
            mesh.Normals = normalList.ToArray();
            return mesh;
        }
        public static ModelMesh LoadObjIndexedTBN(string path) {
            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<Vector3> normalList = new List<Vector3>();
            List<Vector3> tangentList = new List<Vector3>();
            List<Vector3> bitangentList = new List<Vector3>();
            ModelMesh mesh = new ModelMesh();
            ObjLoader.LoadObj(path, vertexList, uvList, normalList);
            TangentSpace.computeTangentBasis(vertexList, uvList, normalList, tangentList, bitangentList);

            List<ushort> out_indices = new List<ushort>();
            List<Vector3> out_vertices = new List<Vector3>();
            List<Vector2> out_uvs = new List<Vector2>();
            List<Vector3> out_normals = new List<Vector3>();
            List<Vector3> out_tangents = new List<Vector3>();
            List<Vector3> out_bitangents = new List<Vector3>();

            VboIndexHelper.indexVboTbn(vertexList, uvList, normalList, tangentList, bitangentList, out_indices, out_vertices, out_uvs, out_normals, out_tangents, out_bitangents);
            mesh.Vertices = out_vertices.ToArray();
            mesh.Uvs = out_uvs.ToArray();
            mesh.Normals = out_normals.ToArray();
            mesh.Tangents = out_tangents.ToArray();
            mesh.BiTangents = out_bitangents.ToArray();
            mesh.Indices = out_indices.ToArray();
            return mesh;

        }
    }
}
